package com.example.administrator.xtu_community.Adapter;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.administrator.xtu_community.R;
import com.example.administrator.xtu_community.activity.InforActivity;
import com.example.administrator.xtu_community.simpleClass.My;

import java.util.List;

public class MeRecyclerViewAdapter extends RecyclerView.Adapter<MeRecyclerViewAdapter.ViewHolder> {
    private static final String TAG = "MeRecyclerViewAdapter";
    private Context mContext;
    private List<My> mMyList;

    static class ViewHolder extends RecyclerView.ViewHolder{
        CardView cardView;
        ImageView myImage;
        TextView myText;

        public ViewHolder(View view){      //定义内部类
            super(view);
            cardView = (CardView) view;
            myImage = (ImageView) view.findViewById(R.id.my_image);
            myText = (TextView) view.findViewById(R.id.my_text);
        }
    }

    public MeRecyclerViewAdapter(List<My> myList){      //传入数据源
        mMyList = myList;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType){    //创建实例
        if (mContext == null){
            mContext = parent.getContext();
        }
        View view = LayoutInflater.from(mContext).inflate(R.layout.my_item,parent,false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position){    //对RecyclerView子项数据进行赋值
        My my = mMyList.get(position);
        holder.myText.setText(my.getName());
        Glide.with(mContext).load(my.getImageId()).into(holder.myImage);
        holder.cardView.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                int position = holder.getBindingAdapterPosition();
                My my = mMyList.get(position);
                Intent intent = new Intent(mContext, InforActivity.class);
                intent.putExtra(InforActivity.INFOR_NAME, my.getName());
                intent.putExtra(InforActivity.INFOR_IMAGE_ID, my.getImageId());
                mContext.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {    //RecyclerView子项数
        return mMyList.size();
    }

}
